package tech.eyao.hs60x.config;

import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.filter.OrderedCharacterEncodingFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import tech.eyao.hs60x.common.CommonConstants;
import tech.eyao.hs60x.filter.AuthenticationFliter;

@Configuration
public class CommonConfig {

    @Bean
    @ConditionalOnMissingBean(CharacterEncodingFilter.class)
    public CharacterEncodingFilter characterEncodingFilter() {
        CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        filter.setForceRequestEncoding(true);
        filter.setForceResponseEncoding(true);
        return filter;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(authenticationFliter());
        List<String> urlPatterns = new ArrayList<String>();
        urlPatterns.add("*.do");
        registrationBean.setUrlPatterns(urlPatterns);
        return registrationBean;
    }

    @Bean
    public AuthenticationFliter authenticationFliter() {
        AuthenticationFliter authenticationFliter = new AuthenticationFliter();
        authenticationFliter.setNoNrl(CommonConstants.LOGIN_METHOD);
        authenticationFliter.setNoNrl(CommonConstants.LOGIN_METHOD2);
        authenticationFliter.setNoNrl(CommonConstants.REGISTER_METHOD);
        authenticationFliter.setNoNrl(CommonConstants.LOGIN_METHOD3);
        return authenticationFliter;
    }

}
